package com.example.zxq_server.whbgy;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.zxq_server.whbgy.fragpack.FType;
import com.example.zxq_server.whbgy.fragpack.IFileCallBack;
import com.example.zxq_server.whbgy.fragpack.MoneyFragment;
import com.example.zxq_server.whbgy.fragpack.PhoneFileFragment;
import com.example.zxq_server.whbgy.fragpack.PhoneFragment;
import com.example.zxq_server.whbgy.fragpack.StepFragment;
import com.example.zxq_server.whbgy.sppack.IPhoneFile;
import com.example.zxq_server.whbgy.sppack.SpFactory;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class MainActivity extends AppCompatActivity implements IFileCallBack {

    IPhoneFile iPhoneFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.host_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Excel联系薄");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        iPhoneFile= SpFactory.creat(this);

        MainActivityPermissionsDispatcher.initPhoneWithCheck(this);

    }

    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE})
    public void initPhone()
    {
        toFragment(FType.phone);
    }

    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE})
    public void repeateRequest()
    {
        MainActivityPermissionsDispatcher.initPhoneWithCheck(this);
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE})
    public void neverBdVoice()
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("用户需知").setMessage("由于您拒绝我们授予我们使用文件读取的权限，以及拨打电话权限，导致程序闪退，如需使用本应用，请卸载本应用后，重新安装！").
                setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
        builder.show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
             case R.id.bt_step:
                toFragment(FType.step);
             break;

             case R.id.bt_file:
                toFragment(FType.file);

                break;
             case R.id.bt_autor:
               toFragment(FType.autor);

                break;
             case R.id.bt_money:
                toFragment(FType.money);
                break;
            case R.id.bt_phone:
                toFragment(FType.phone);
                break;


        }
        return super.onOptionsItemSelected(item);
    }


    private void toFragment(FType fType)
    {
        switch (fType)
        {
            case autor:
//                AutorFragment autorFragment=new AutorFragment();
//                toFragment(autorFragment);
                PhoneFragment localPhone= PhoneFragment.newInstance("");
                toFragment(localPhone);

                break;
            case money:
                MoneyFragment moneyFragment=new MoneyFragment();
                toFragment(moneyFragment);
                break;
            case phone:
                PhoneFragment phoneFragment =   PhoneFragment.newInstance(iPhoneFile.getPhoneFile());
                toFragment(phoneFragment);
                break;
            case file:
                PhoneFileFragment fileFragment=new PhoneFileFragment();
                fileFragment.setiFileCallBack(this);
                toFragment(fileFragment);
                break;
            case step:
                StepFragment stepFragment=new StepFragment();
                toFragment(stepFragment);
                break;

        }

    }


    private void toFragment(Fragment fragment)
    {
        getSupportFragmentManager().beginTransaction().replace(R.id.myFrame,fragment).commit();
    }


    @Override
    public void setFilePath(String filePath) {
      //  Toast.makeText(this,"ss",Toast.LENGTH_LONG).show();
        iPhoneFile.setPhoneFile(filePath);
        toFragment(FType.phone);
    }


}
